package com.procurement.bidding.service.impl;

import com.procurement.bidding.domain.PcmEvaluationRuleDetail;
import com.procurement.bidding.mapper.*;
import com.procurement.bidding.service.PcmExpertSupplierService;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PcmExpertSupplierServiceImpl implements PcmExpertSupplierService {
    @Resource
    private PcmExpertSupplierMapper pesm;
    @Resource
    private PcmEvaluationRuleDetailMapper perdm;
    @Resource
    private PcmPublicAttachmentMapper ppam;
    @Resource
    private PcmBiddingMiddleMapper pbmm;
    @Resource
    private PcmInvitedSupplierMapper pism;
    @Resource
    private PcmAuditMapper pam;

    @Override
    public Integer supplierBidding(Map<String, Object> map) {
        Long middleId = Long.valueOf(map.get("middleId").toString());
        Long supplierId = pbmm.findBySupplierId(SecurityUtils.getUserId());
        Integer updateIsBid = pesm.updateIsBid(middleId, supplierId);
        List<Map<String,Object>> pcmEvaluationRuleDetailList = (List<Map<String,Object>>) map.get("pcmEvaluationRuleDetailList");
        pcmEvaluationRuleDetailList.forEach(item->{
            System.out.println(item+"************//////////***********/////////***********");
            perdm.updateBidContentSummary(item);
        });
        Long invitationSupplierId = pism.selByMiddleIdSupplierId(middleId, supplierId);
        map.put("invitationSupplierId",invitationSupplierId);
        ppam.delInvitationSupplierId(invitationSupplierId);
        Integer addInvitationSupplierBooks = ppam.addInvitationSupplierBooks(map);
        return updateIsBid+addInvitationSupplierBooks;
    }

    @Override
    public Boolean expertEvaluation(Map<String, Object> map) {
        List<Map<String,Object>> pdList = (List<Map<String,Object>>) map.get("pcmEvaluationRuleDetailList");
        //修改评标理由和得分
        pdList.forEach(pd ->  perdm.updateReasonScore(pd));
        Long auditId = Long.valueOf(map.get("auditId").toString());
        //结束专家评标待办
        Integer updateState = pam.updateState(auditId);
        Long expertSupplierId = Long.valueOf(map.get("expertSupplierId").toString());
        Integer updateIsEvaluation = pesm.updateIsEvaluation(expertSupplierId);
        return updateState+updateIsEvaluation>0;
    }
}
